<template>
  <div class="public_main">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>{{$t('newcrumbs.crumbsEle')}}</el-breadcrumb-item>
        <el-breadcrumb-item>{{$t('newcrumbs.crumbsTwe')}}</el-breadcrumb-item>
        <el-breadcrumb-item>{{$t('add.no45')}}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    

    <!-- -------------搜索查询栏------------------------ -->
    <search 
      :searchData="searchData" 
      @search="search" 
      :searchRight="true"
    ></search>

    <div class="list_operation" v-if="$store.state.common.permiss.ruleList.includes('ruleListA')">
      <el-button 
        type="primary" 
        @click="showAdd">
        +{{$t('rule.no8')}}
      </el-button>
    </div>

    <!-- -------------表单显示栏------------------------ -->
    <div class="table" >
      <template>
        <el-table :data="tableData" size="small" >
          <el-table-column align="center" prop="id" :label="$t('rule.no1')">
          </el-table-column>
          <el-table-column align="center" prop="ruleSetName" :label="$t('rule.no5')">
          </el-table-column>
          <el-table-column align="center" prop="userType" :label="$t('add.no86')">
            <template slot-scope="scope">
              <span>{{scope.row.userType==1?$t('add.no83'):scope.row.userType==2?$t('add.no84'):''}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="riskUserLevelSet" :label="$t('rule.no2')">
          </el-table-column>
          <el-table-column align="center" prop="effective" :label="$t('userSuggest.status')">
            <template slot-scope="scope">
              <span>{{scope.row.effective==1?$t('rule.no3'):scope.row.effective==0?$t('rule.no4'):''}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="effectiveCount" :label="$t('rule.no9')">
          </el-table-column>
          <el-table-column fixed="right" align="center" prop="operation" :label="$t('public.operation')" min-width="160">
            <template slot-scope="scope">
              <span 
                v-if="$store.state.common.permiss.ruleList.includes('ruleListM')"
                class="table_opr"
                @click="showModify(scope.row.id,scope.row.riskUserLevelSet,scope.row.ruleSetName,scope.row.userType)">
                {{$t('public.no51')}}
              </span>
              <span 
                v-if="$store.state.common.permiss.ruleList.includes('ruleListAA')"
                class="table_opr"
                @click="jumpAddRule(scope.row.id,scope.row.ruleSetName)">
                {{$t('rule.no6')}}
              </span>
              <span 
                v-if="$store.state.common.permiss.ruleList.includes('ruleListS')"
                class="table_opr"
                @click="jumpDetailRule(scope.row.id,scope.row.ruleSetName)">
                {{$t('rule.no7')}}
              </span>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>
    <!-- ------------  分页显示栏  ------------------------ -->
    <!-- <el-row type="flex" justify="end">
      <div class="pages" >
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pagination.current"
          layout="sizes, prev, pager, next, total,->"
          :page-sizes="pagination.pageSizeOptions"
          :page-size="pagination.pageSize"
          @size-change="handleSizeChange"
          :total="pagination.total">
        </el-pagination>
      </div>
    </el-row> -->
    
    <el-dialog :title="$t('proManage.no4')" :visible.sync="dialogFlag" width="560" top="20vh">
      <el-form :model="ruleForm" size="small" ref="ruleForm" label-width="130px">
        <el-form-item :label="$t('rule.no5')">
          <el-input v-model="ruleForm.ruleSetName"></el-input>
        </el-form-item>
        <el-form-item :label="$t('add.no86')">
          <el-radio-group v-model="ruleForm.userType">
            <el-radio :label="1">{{$t('add.no83')}}</el-radio>
            <el-radio :label="2">{{$t('add.no84')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('rule.no2')" v-if="ruleForm.userType==2">
          <el-checkbox-group v-model="ruleForm.riskUserLevels" v-if="isAddFlag" >
            <div class="types">
              <ul>
                <li v-for="(item,i) in userGradeOptions" :key="i">
                  <el-checkbox :label="item.value" disabled ></el-checkbox>
                </li>
              </ul>
            </div>
          </el-checkbox-group>
          <el-checkbox-group v-model="ruleForm.riskUserLevels" v-if="isModifyFlag">
            <div class="types">
              <ul>
                <li v-for="(item,i) in userGradeOptions" :key="i">
                  <el-checkbox :label="item.value" :disabled="item.disabled" ></el-checkbox>
                </li>
              </ul>
            </div>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item :label="$t('rule.no2')" v-if="ruleForm.userType==1">
          <el-checkbox-group v-model="ruleForm.riskUserLevels" v-if="isAddFlag" >
            <div class="types">
              <ul>
                <li v-for="(item,i) in userGradeNewOptions" :key="i">
                  <el-checkbox :label="item.value" disabled ></el-checkbox>
                </li>
              </ul>
            </div>
          </el-checkbox-group>
          <el-checkbox-group v-model="ruleForm.riskUserLevels" v-if="isModifyFlag">
            <div class="types">
              <ul>
                <li v-for="(item,i) in userGradeNewOptions" :key="i">
                  <el-checkbox :label="item.value" :disabled="item.disabled" ></el-checkbox>
                </li>
              </ul>
            </div>
          </el-checkbox-group>
        </el-form-item>
        <!-- <p class="pl120 red" v-for="(item,i) in userTypeLabel" :key="i">
          {{item.userLoanGrade}} 原执行规则集 {{item.ruleSetName}};
        </p> -->
      </el-form>
      <div class="button">
        <el-button type="primary" @click="submit">{{$t('public.no49')}}</el-button>
        <el-button type="primary" @click="detailClose">{{$t('public.no50')}}</el-button>
      </div>
    </el-dialog>

    <div class="foot"></div>

  </div>
</template>

<script>
export default {
  name: 'ruleEngine',
  data () {
    const searchData=[
      {
        type: 'input',
        label: 'rule.no1',
        attr: 'id',
        value: ''
      },{
        type: 'select',
        label: 'userSuggest.status',
        attr: 'effective',
        option: this.$store.state.options.rulestatus_option,
        value: '',
        langFlag: true,
      },{
        type: 'select',
        label: 'add.no86',
        attr: 'userType',
        option: this.$store.state.options.ruleuserType_option,
        value: '',
        langFlag: true,
      },{
        type: 'select',
        label: 'rule.no2',
        attr: 'stage',
        option: [],
        value: '',
        langFlag: false,
      }]
    return {
      flag: true,
      sessionid: '',
      searchData: searchData,
      formInline: {},// 用户查询信息数据对应字段
      tableData: [],
      detailFlag: false,
      options1: [
        {label:'请选择',value:''},
        {id:1,label:'启用',value:1},
        {id:-1,label:'关闭',value:-1},
      ],
      options2: [
        {label:'请选择',value:''},
        {id:1,label:'新客',value:1},
        {id:2,label:'老客',value:2},
      ],
      options3: [],
      userGradeOptions: [],
      userGradeNewOptions: [],
      userGradeAllOptions: [],
      dialogFlag: false,// 添加修改弹窗
      ruleForm: {
        id: '',
        ruleSetName: '',
        userType: '',
        riskUserLevels: [],
      },
      isAddFlag: false,
      isModifyFlag: false,
      isDisabled: false,
      userTypeLabel: [],
      pagination: {
        pageSize: global.config.pageSize,
        pageSizeOptions: global.config.pageSizeOptions,
        current: 1,
        total: 0,
      },
    }
  },
  methods: {
    handleSizeChange (val) {// 每页条数变化时操作
      this.pagination.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange (val) { // 分页按钮点击操作
      this.pagination.current = val;
      this.getTableData();
    },
    getTableData () { // 获取列表数据
      let option = {
        pageNum:this.pagination.current,
        pageSize:this.pagination.pageSize,
        param:{
          ...this.formInline
        },
      }
      // this.$axios.post(this.$action.risk_rule_set,option).then(res=>{
      this.$axios.post(this.$action.risk_rule_set,option).then(res=>{
        if(res.code==200){
          this.tableData = res.data;
          // this.pagination.total = res.data.total;
        }
      })
    },
    search(search) { // 搜索
      this.formInline = {
        id: search.id,
        userType: search.userType,
        levelType:search.levelType,
        effective: search.effective,
      };
      this.pagination.current = 1;
      this.getTableData();
    },
    submit(){
      let httpUrl = this.$action.risk_rule_creation;
      let option = {
        param: {
          ...this.ruleForm
        },
      }
      if(this.ruleForm.id){
        httpUrl = this.$action.risk_set_modification
        // option.param.id = this.ruleForm.id
      }
      this.$axios.post(httpUrl,option).then(res=>{
        if (res.code == 200) {
          this.$globalMsg.success(this.$t('message.success'));
          this.getTableData();
          this.detailClose();
        } else {
          this.$globalMsg.error(res.message);
        }
      })
    },
    detailClose(){
      this.dialogFlag = false;
      this.isModifyFlag = false;
      this.isAddFlag = false;
      this.ruleForm.id = '';
      this.ruleForm.ruleSetName = '';
      this.ruleForm.userType = '';
      this.ruleForm.riskUserLevels = [];
      this.userTypeLabel = [];
      this.userGradeOptions.forEach(item=>{
          item.disabled = false;
      })
      this.userGradeNewOptions.forEach(item=>{
          item.disabled = false;
      })
    },
    showAdd(){
      this.isAddFlag = true;
      this.dialogFlag = true;
    },
    showModify(id,riskUserLevelSet,ruleSetName,userType){
      this.ruleForm.id = id;
      this.ruleForm.ruleSetName = ruleSetName;
      this.ruleForm.userType = userType;
      this.ruleForm.riskUserLevels = riskUserLevelSet?riskUserLevelSet.split(','):[];
      let haveGrade = riskUserLevelSet?riskUserLevelSet.split(','):[]
      if(userType==2){
        this.userGradeOptions.forEach(item=>{
          if(haveGrade.includes(item.value)){
            item.disabled = true;
          }
        })
      }
      if(userType==1){
        this.userGradeNewOptions.forEach(item=>{
          if(haveGrade.includes(item.value)){
            item.disabled = true;
          }
        })
      }
      
      
      this.isModifyFlag = true;
      this.dialogFlag = true;
    },
    jumpAddRule(ruleSetId,ruleSetName){
      this.$router.push({
        path: '/AddRuleEngine',
        query: {
          ruleSetId,ruleSetName
        }
      })
    },
    jumpDetailRule(ruleSetId,ruleSetName,updateTime,isExcute){
      this.$router.push({
        path: '/ruleDetailList',
        query: {
          ruleSetId,ruleSetName,updateTime,isExcute
        }
      })
    },
    getsexStatus () { // 获取app名字和包名
      let option = {
        header: {
          ...this.$base,
          action: this.$store.state.actionMap.back_reason,
          'sessionid': this.sessionid
        },
        optionGroup:'sex'
      }
      this.$axios.post('', option).then(res => {
        if (res.data.header.code == 0) {
          let arr = res.data.data;
          arr.forEach(value=>{
            value.label = value.optionName;
            value.value = value.optionName;
          })
          this.options2 = arr;
        }
      })
    },
    select(){
      this.getTableData()
    },
    getsuerGrade () { // 获取老用户等级
      this.userGradeOptions = [];
      let option = {
        param: {
          userType: 2
        },
      }
      this.$axios.post(this.$action.risk_user_level, option).then(res => {
        if (res.code == 200) {
          let arr = [];
          res.data.forEach(value=>{
            arr.push({label:value,value:value,disabled: false})
            // value.disabled = false;
          })
          this.userGradeOptions = arr;
        }
      })
    },
    getsuerGradeAll () { // 获取全部下拉框用户等级
      this.userGradeOptions = [];
      let option = {
        param: {}
      }
      this.$axios.post(this.$action.risk_user_level, option).then(res => {
        if (res.code == 200) {
          let arr = [];
          res.data.forEach(value=>{
            arr.push({label:value,value:value})
            // value.disabled = false;
          })
          // this.searchData.filter(value=>value.attr=='levelType')[0].option = arr
          this.searchData.filter(value=>value.attr=='stage')[0].option = arr
          this.userGradeAllOptions = arr;
        }
      })
    },
    getsuerGradeNew () { // 获取新用户等级
      this.userGradeOptions = [];
      let option = {
        param: {
          userType: 1
        },
      }
      this.$axios.post(this.$action.risk_user_level, option).then(res => {
        if (res.code == 200) {
          let arr = [];
          res.data.forEach(value=>{
            arr.push({label:value,value:value,disabled:false})
            // value.disabled = false;
          })
          this.userGradeNewOptions = arr;
        }
      })
    }
  },
  watch: {
    dialogFlag(){
      if(!this.dialogFlag){
        this.detailClose()
      }
    },
    'ruleForm.userType'(){
      this.ruleForm.riskUserLevels = []
    },

  },
  mounted () {
    this.getTableData();
    this.getsuerGrade();
    this.getsuerGradeAll();
    this.getsuerGradeNew();
    
  }
}
</script>

<style scoped lang="scss">
  .ruletable{
    width: 100%;
  }
  .box{
    width: 100%;
  }
  table{
    margin-bottom: 1em;
    overflow: auto;
    box-sizing: border-box;
    display: table;
    text-shadow: 0px 1px 0px #fff;
    border-collapse: collapse;
    border-spacing: 0;
    color: #292e31;
    font-size: 14px;
    tr:last-child {
        border-bottom: 1px solid #ccc;
    }
    tr {
        display: table-row;
        vertical-align: inherit;
        border-color: inherit;
    }
    th {
        padding: 5px 10px;
        border-bottom: 1px solid #ccc;
        vertical-align: bottom;
        line-height: 1.6;
        text-align: left;
        font-weight: bold;
        display: table-cell;
    }
    tr:nth-child(odd)>td {
        background-color: #f8f8f8;
    }
    td.nowrap {
        word-break: keep-all;
        white-space: nowrap;
    }
    td{
        display: table-cell;
        padding: 10px;
        vertical-align: top;
        line-height: 1.6;
    }
  }
  table label.el-radio {
    margin-right: 10px;
  }
  .button {
    display: flex;
    justify-content: center;
    button: {
      margin: 0 20px;
    }
  }
  .types{
    width: 100%;
    ul{
      display: flex;
      flex-wrap: wrap;
      li{
        width: 56px;
        margin-right: 20px;
      }
    }
  }
  .pl120{
    padding-left: 120px;
    margin-bottom: 16px;
  }
</style>
